﻿using GalaSoft.MvvmLight;
using WP7Contrib.Services.Navigation;
using WP7Contrib.Services.Storage;
using WP7Contrib.Services.Settings;
using GalaSoft.MvvmLight.Messaging;
using Coding4Fun.Phone.Controls.Data;
using GalaSoft.MvvmLight.Command;
using DepSoft.Mobile.WP7.FollwIt.Framework;
using DepSoft.Mobile.WP7.FollwIt.Model;
using System;
using WindowsPhonePostClient;
using System.Windows;

namespace DepSoft.Mobile.WP7.FollwIt.ViewModel
{ 
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class SettingsViewModel : ViewModelBaseWp7
    {
        #region Public properties

        public string ApplicationTitle
        {
            get
            {
                return PhoneHelper.GetAppAttribute("Title").ToUpper();
            }
        }

        public string PageName
        {
            get
            {
                return "settings";
            }
        }

        public RelayCommand SaveCommand { get; private set; }
        #endregion

        #region Public bindable properties
        #region UserName
        /// <summary>
        /// The <see cref="UserName" /> property's name.
        /// </summary>
        public const string UserNamePropertyName = "UserName";

        private string _userName = string.Empty;

        /// <summary>
        /// Sets and gets the UserName property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string UserName
        {
            get
            {
                return _userName;
            }

            set
            {
                if (_userName == value)
                {
                    return;
                }

                _userName = value;
                RaisePropertyChanged(UserNamePropertyName);
            }
        }
        #endregion

        #region Password
        /// <summary>
        /// The <see cref="Password" /> property's name.
        /// </summary>
        public const string PasswordPropertyName = "Password";

        private string _password = string.Empty;

        /// <summary>
        /// Sets and gets the Password property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }

            set
            {
                if (_password == value)
                {
                    return;
                }

                _password = value;
                RaisePropertyChanged(PasswordPropertyName);
            }
        }
        #endregion
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the SettingsViewModel class.
        /// </summary>
        public SettingsViewModel(INavigationService navigationService, IStorageService storageService, IStoreSettings settingsService, IMessenger messenger)
            : base(navigationService, storageService, settingsService, messenger)
        {
            #region Command binding
            this.SaveCommand = new RelayCommand(() => this.Save());
            #endregion

            this.UserName = FollwItApi.GetInstance().UserName;
            this.Password = FollwItApi.GetInstance().Password;
        }
        #endregion

        #region Private methods
        private void Save()
        {
            FollwItApi.GetInstance().UserName = this.UserName;
            FollwItApi.GetInstance().Password = this.Password;

            FollwItApi.GetInstance().AuthenticateCompleted += new System.EventHandler(SettingsViewModel_AuthenticateCompleted);

            //First validate the user, to see if these settings are ok
            FollwItApi.GetInstance().AuthenticateAsync();            
        }

        void SettingsViewModel_AuthenticateCompleted(object sender, EventArgs e)
        {
            FollwItApi.GetInstance().AuthenticateCompleted -= new System.EventHandler(SettingsViewModel_AuthenticateCompleted);
            if (((DownloadStringCompletedEventArgs)e).Result.Contains("success"))
            {
                this.MessengerInstance.Send(new NotificationMessage("AuthenticateComplete"));
                NavigationService.GoBack();
            }
            else
            {
                FollwItApi.GetInstance().UserName = string.Empty;
                FollwItApi.GetInstance().Password = string.Empty;
                MessageBox.Show("Login failed for the Follw.it service!" + Environment.NewLine + "Please check your credentials...", "Error", MessageBoxButton.OK);
            }
        }
        #endregion

        #region ViewModelBaseWp7 implementation
        protected override void IsBeingActivated(WP7Contrib.Services.Storage.IStorage storage)
        {
            throw new System.NotImplementedException();
        }

        protected override void IsBeingDeactivated(WP7Contrib.Services.Storage.IStorage storage)
        {
            throw new System.NotImplementedException();
        }
        #endregion
    }
}